package com.yww.leetcode2.stack_queue;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author yww
 * @description 225. 用队列实现栈
 * @since 2024/11/20
 */
public class MyStack225_02 {
    private final Queue<Integer> queue = new LinkedList<Integer>();

    public MyStack225_02() {

    }

    public void push(int x) {
        queue.offer(x);
    }

    public int pop() {
        int size = queue.size();
        size--;
        while (size-- > 0) {
            queue.offer(queue.poll());
        }
        return queue.poll();
    }

    public int top() {
        int size = queue.size();
        Integer top = null;
        size--;
        while (size-- > 0) {
            queue.offer(queue.poll());
        }
        top = queue.poll();
        queue.offer(top);

        size = queue.size();
        while (size-- > 0) {
            queue.offer(queue.poll());
        }
        return top;
    }

    public boolean empty() {
        return queue.isEmpty();
    }
}
